//
//  LssWelcomeViewController.swift
//  freeChatting
//
//  Created by mac on 16/4/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage
class LssWelcomeViewController: UIViewController {
//    override func loadView() {
//        view = backgroundImg
//    }

    override func viewDidLoad() {
        super.viewDidLoad()
      view = backgroundImg
        setUpUI()
    }
    //MARK:- 设置UI控件
    func setUpUI(){
//        view.addSubview(backgroundImg)
        view.addSubview(messageLabel)
        view.addSubview(iconImageView)
        //设置约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(15)
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        makeAnimations()
    }
    //MARK:- 加动画效果
    func makeAnimations(){
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        //阻尼动画
    UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
        self.view.layoutIfNeeded()
        }) { (_) -> Void in
//            UIView.animateWithDuration(0.25, animations: { () -> Void in
//                self.messageLabel.alpha = 1
//            })
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.messageLabel.alpha = 1
                }, completion: { (_) -> Void in
                    //发送通知，切换根视图
                    NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootViewController", object: "welcome")
            })
        }
    }
    //背景图片的懒加载
    private lazy var backgroundImg: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像图片的懒加载
    private lazy var iconImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "avatar_default_big")
        imgView.layer.cornerRadius = 85 / 2
        imgView.layer.borderColor = themeColor.CGColor
        imgView.layer.borderWidth = 2
        imgView.clipsToBounds = true
        imgView.sd_setImageWithURL(NSURL(string: (LssAppAccountTools.sharedAccountTools.userAccount?.avatar_large) ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return imgView
        }()
    //“欢迎回来”标签的懒加载
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), textFont: 20)
        label.text = "欢迎回来"
        label.textAlignment = NSTextAlignment.Center
        label.alpha = 0
        return label
        }()
}
